using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProduceBasicData.Api.Application.Command.ProcessReport;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.ErrorCode;

namespace ProduceBasicData.Api.Controllers
{
    /// <summary>
    /// 工序报工数据控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProcessReportController : ControllerBase
    {
        private readonly IMediator mediator;

        public ProcessReportController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取工序报工数量图表数据接口
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns>工序报工数量图表数据</returns>
        [HttpGet]
        public async Task<ApiResult<ProcessReportChartDto>> GetProcessReportQuantity([FromQuery] GetProcessReportQuantityCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取工序报工数量图表数据接口（简化版本，无参数）
        /// </summary>
        /// <returns>工序报工数量图表数据</returns>
        [HttpGet]
        public async Task<ApiResult<ProcessReportChartDto>> GetProcessReportQuantitySimple()
        {
            var request = new GetProcessReportQuantityCommand
            {
                SortType = 2, // 按数量降序
                OnlyWithReports = true
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取本月工序报工数量图表数据接口
        /// </summary>
        /// <returns>本月工序报工数量图表数据</returns>
        [HttpGet]
        public async Task<ApiResult<ProcessReportChartDto>> GetCurrentMonthProcessReportQuantity()
        {
            var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            var endDate = DateTime.Now;

            var request = new GetProcessReportQuantityCommand
            {
                StartDate = startDate,
                EndDate = endDate,
                SortType = 2, // 按数量降序
                OnlyWithReports = true
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取工艺报工数量图表数据接口
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns>工艺报工数量图表数据</returns>
        [HttpGet]
        public async Task<ApiResult<CraftReportChartDto>> GetCraftReportQuantity([FromQuery] GetCraftReportQuantityCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取工艺报工数量图表数据接口（简化版本，无参数）
        /// </summary>
        /// <returns>工艺报工数量图表数据</returns>
        [HttpGet]
        public async Task<ApiResult<CraftReportChartDto>> GetCraftReportQuantitySimple()
        {
            var request = new GetCraftReportQuantityCommand
            {
                SortType = 2, // 按数量降序
                OnlyWithReports = true
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取本月工艺报工数量图表数据接口
        /// </summary>
        /// <returns>本月工艺报工数量图表数据</returns>
        [HttpGet]
        public async Task<ApiResult<CraftReportChartDto>> GetCurrentMonthCraftReportQuantity()
        {
            var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            var endDate = DateTime.Now;

            var request = new GetCraftReportQuantityCommand
            {
                StartDate = startDate,
                EndDate = endDate,
                SortType = 2, // 按数量降序
                OnlyWithReports = true
            };
            return await mediator.Send(request);
        }
    }
}
